package com.supcon.exchangedata.service.hj;

import com.supcon.exchangedata.pojo.entity.oracle.CtCar;
import com.supcon.exchangedata.pojo.entity.mysql.operation.hj.HjEnvironmentCars;
import com.supcon.exchangedata.repository.master.MasterCtCareRepository;
import com.supcon.exchangedata.repository.slave6.SlaveHjBasicInfo5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 环境车辆信息
 *
 * @Author: yangbin
 * @Date: 2022/03/17/15:07
 * @Description:
 */
@Service
@Slf4j
public class GarbageCarService {
    @Autowired
    private SlaveHjBasicInfo5 slaveHjBasicInfo5;
    @Autowired
    private MasterCtCareRepository masterCtCareRepository;

    /**
     * 环境车辆信息同步
     */
    @Transactional(rollbackFor = Exception.class)
    public void tranGarbageCar() {
        masterCtCareRepository.deleteAllByGroupId(4L);
        List<HjEnvironmentCars> all = slaveHjBasicInfo5.findAll();
        if (!CollectionUtils.isEmpty(all)) {
            all.forEach((e) -> {
                CtCar ctCar = new CtCar();
                //车牌号
                String carLicense = e.getCarLicense();
               /* CtCar byLicensePlateNum = masterCtCareRepository.findByLicensePlateNum(carLicense);*/

                /*if (byLicensePlateNum != null) {
                    ctCar = byLicensePlateNum;
                }*/

                ctCar.setGroupId(4L);
                ctCar.setUpdateTime(LocalDateTime.now());
                ctCar.setLicensePlateNum(carLicense);

                //车辆型号
                String carModel = e.getCarModel();
                ctCar.setModel(carModel);

                //车辆原价
                String price = e.getPrice();
                //排放标准
                String emissionStandard = e.getEmissionStandard();
                //底盘商家
                String brandChassis = e.getBrandChassis();
                //总质量
                String carWeight = e.getCarWeight();
                //车辆自编号
                String carSerialNumber = e.getCarSerialNumber();
                ctCar.setCarCode(carSerialNumber);
                //排量
                String displacement = e.getDisplacement();
                //车身颜色
                String carColour = e.getCarColour();
                //外尺寸
                String carSize = e.getCarSize();
                //性质
                String nature = e.getNature();
                ctCar.setUseType(nature);


                //空车重量
                String carLoad = e.getCarLoad();
                //发动机编号
                String engineNumber = e.getEngineNumber();
                ctCar.setEngineNumber(engineNumber);

                //发动机型号
                String engineModel = e.getEngineModel();


                //上装厂家
                String barnd = e.getBarnd();
                //使用状态
                String useStatus = e.getUseStatus();
                //轮胎规格
                String tyreSize = e.getTyreSize();
                //机务状态
                String maintenanceStatus = e.getMaintenanceStatus();
                //吨位
                String tonnage = e.getTonnage();
                if(!ObjectUtils.isEmpty(tonnage) && (tonnage.contains("."))){
                    tonnage = tonnage.substring(0, tonnage.indexOf("."));
                    ctCar.setLoadLimitNum(Long.valueOf(tonnage));
                }


                masterCtCareRepository.save(ctCar);
            });
        }


    }

}
